{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ea635acd",
   "metadata": {},
   "source": [
    "# 模块math：数学"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ee36788b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "07c87e76",
   "metadata": {},
   "source": [
    "math模块的主要功能是计算数学函数。例如：计算平方根："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "29097530",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.sqrt(16)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "bd3e3572",
   "metadata": {},
   "source": [
    "除了数学函数之外，模块中还有一些数学常数。例如，圆周率："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "febe901b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.141592653589793"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.pi"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b9fcefa",
   "metadata": {},
   "source": [
    "自然底数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "90b72f89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.718281828459045"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.e"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "5723e7c5",
   "metadata": {},
   "source": [
    "三角函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ca0c108c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.cos(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ef48deaf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.sin(math.pi / 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f5e6036a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9999999999999999"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.tan(math.pi / 4)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c6bc81ea",
   "metadata": {},
   "source": [
    "反三角函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fcb6d96e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.5707963267948966"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.asin(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "65ff3410",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.5707963267948966"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.acos(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "055ce7b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7853981633974483"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.atan(1)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "173b745a",
   "metadata": {},
   "source": [
    "对数和指数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ed69c77d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.302585092994046"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.log(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "10061eee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10.000000000000002"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.exp(math.log(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "38607a4f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6931471805599453"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.log1p(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "11442483",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.expm1(0.6931471805599453)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "47b74f94",
   "metadata": {},
   "source": [
    "近似，向上取整和向下取整："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "58c34dc0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.ceil(10.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8d22d1e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.floor(10.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e6dc292",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
